﻿using ShiQuan.IotDevice.Entity;
using ShiQuan.Provider;
using ShiQuan.Utility;
using ShiQuan.WebControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ShiQuan.IotDevice.Business
{
    /// <summary>
    /// 设备安装业务处理
    /// </summary>
    public class IotDeviceInstallBLL:BaseIotDeviceBLL<IotDeviceInstallEntity>
    {
        /// <summary>
        /// 获取树状列表
        /// </summary>
        /// <param name="queryJson"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<TreeModel> GetTreeList(string queryJson, bool root = true)
        {
            var data = this.GetList(queryJson);

            var treeList = new List<TreeModel>();
            TreeModel rootModel = null;
            if (root)
            {
                rootModel = new TreeModel();
                bool hasChildren = data.Count() == 0 ? false : true;
                rootModel.id = "-1";
                rootModel.text = "所有设备";
                rootModel.value = "-1";
                rootModel.isexpand = true;
                rootModel.complete = true;
                rootModel.hasChildren = hasChildren;
                rootModel.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(rootModel);
            }
            foreach (IotDeviceInstallEntity item in data)
            {
                TreeModel tree = new TreeModel();
                tree.id = item.Id;
                tree.text = item.DeviceName + "(" + item.InstallCode + ")";
                tree.value = item.Id;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                if (rootModel != null)
                    tree.parentId = rootModel.id;
                else
                    tree.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(tree);
            }
            return treeList;
        }
        /// <summary>
        /// 设备安装
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public IotDeviceInstallEntity GetNewEntity(string customerId,string deviceId)
        {
            IotDeviceInstallEntity entity = new IotDeviceInstallEntity();
            entity.InstallCode = DateTime.Now.ToString("yyyyMMdd-HHmmss");
            entity.InstalledOn = DateTime.Now;
            entity.InstalledBy = OperatorProvider.Provider.Current().UserName;
            if (string.IsNullOrEmpty(customerId) == false)
            {
                CustomerInfoEntity customerinfo = new CustomerInfoBLL().GetEntity(customerId);
                if (customerinfo != null)
                {
                    entity.RefCorpId = customerinfo.Id;
                    entity.CorpName = customerinfo.Name;
                }
            }
            if (string.IsNullOrEmpty(deviceId) == false)
            {
                IotDeviceInfoEntity deviceInfo = new IotDeviceInfoBLL().GetEntity(deviceId);
                if(deviceInfo != null)
                {
                    entity.RefDeviceId = deviceInfo.Id;
                    entity.DeviceName = deviceInfo.Name;
                }
            }
            return entity;
        }
        /// <summary>
        /// 数据保存
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        public override void SaveForm(string keyValue, IotDeviceInstallEntity entity)
        {
            if(string.IsNullOrEmpty(entity.SerialNumber) == false)
            {
                var count = this.GetList(my => my.Id != keyValue && my.SerialNumber == entity.SerialNumber).Count();
                if (count > 0)
                {
                    throw new Exception("设备标识不能重复！");
                }
            }
            
            if (string.IsNullOrEmpty(entity.RefCorpId))
            {
                throw new Exception("所属客户不能为空！");
            }
            if (string.IsNullOrEmpty(entity.RefDeviceId))
            {
                throw new Exception("设备信息不能为空！");
            }

            if (string.IsNullOrEmpty(entity.RefAreaId))
            {
                CustomerInfoEntity customerInfo = new CustomerInfoBLL().GetEntity(entity.RefCorpId);
                if (customerInfo == null)
                    throw new Exception("未能找到客户信息！");

                entity.RefAreaId = customerInfo.RefAreaId;
                entity.ProvinceName = customerInfo.ProvinceName;
                entity.CityName = customerInfo.CityName;
                entity.AreaName = customerInfo.AreaName;
                entity.StreetName = customerInfo.StreetName;
                entity.InstalledAddr = customerInfo.Address;
            }

            if (string.IsNullOrEmpty(entity.DeviceType))
            {
                IotDeviceInfoEntity deviceInfo = new IotDeviceInfoBLL().GetEntity(entity.RefDeviceId);
                if (deviceInfo == null)
                    throw new Exception("未能找到设备信息！");
                entity.DeviceType = deviceInfo.TypeId;
                //entity.TypeName = deviceInfo.TypeName;
                if (string.IsNullOrEmpty(entity.RefSupplierId))
                {
                    entity.RefSupplierId = deviceInfo.RefSupplierId;
                    entity.SupplierName = deviceInfo.SupplierName;
                }
            }
            if (string.IsNullOrEmpty(keyValue))
            {
                if (string.IsNullOrEmpty(entity.Id))
                    entity.Id = Guid.NewGuid().ToString();

                entity.CreateDate = DateTime.Now;
                entity.CreateUserId = OperatorProvider.Provider.Current().UserId;
                entity.CreateUserName = OperatorProvider.Provider.Current().UserName;

                entity.EnterpriseId = OperatorProvider.Provider.Current().CompanyId;
                entity.EnterpriseName = OperatorProvider.Provider.Current().CompanyName;

                this.Repository.Insert<IotDeviceInstallEntity>(entity);
            }
            else
            {
                entity.Id = keyValue;

                entity.ModifyDate = DateTime.Now;
                entity.ModifyUserId = OperatorProvider.Provider.Current().UserId;
                entity.ModifyUserName = OperatorProvider.Provider.Current().UserName;

                this.Repository.Update<IotDeviceInstallEntity>(entity);
            }
        }
        /// <summary>
        /// 查询过滤
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        protected override Expression<Func<IotDeviceInstallEntity, bool>> QueryStringToExpression(Expression<Func<IotDeviceInstallEntity, bool>> expression, string queryJson)
        {
            if (string.IsNullOrEmpty(queryJson))
                return expression;
            var queryParam = queryJson.ToJObject();
            if(queryParam["RefCorpId"].IsEmpty() == false && queryParam["RefCorpId"].ToString() != "-1")
            {
                var keyword = queryParam["RefCorpId"].ToString();
                expression = expression.And(my => my.RefCorpId == keyword);
            }
            if (queryParam["RefDeviceId"].IsEmpty() == false && queryParam["RefDeviceId"].ToString() != "-1")
            {
                var keyword = queryParam["RefDeviceId"].ToString();
                expression = expression.And(my => my.RefDeviceId == keyword);
            }
            if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
            {
                string condition = queryParam["condition"].ToString();
                string keyword = queryParam["keyword"].ToString();
                switch (condition)
                {
                    default: break;
                }
            }
            return expression;
        }
    }
}
